home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / NUMBERS.SWG / 0014_HEXINFO.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  2KB  |  41 lines

  1. > I am learning Pascal and don't understand something.  How does the
  2. > following Function make a Word into Hex:
  3.  
  4.  It's Really doing two things, it's converting a binary value
  5.  into ascii, and from decimal to hex.  Let's start With the
  6.  calling or main part of the Program.  You're taking a 2 Byte
  7.  Word and breaking it up into 4 nibbles of 4 bits each.  Each of
  8.  these nibbles is displayed as a Single hex Character 0-F.
  9.  
  10.                                    Hex Representation XXXX
  11.                                                       ||||
  12. HexStr := HexStr + Translate(Hi(W) shr 4); -----------||||
  13. HexStr := HexStr + Translate(Hi(W) and 15);------------|||
  14. HexStr := HexStr + Translate(Lo(W) shr 4); -------------||
  15. HexStr := HexStr + Translate(Lo(W) and 15);--------------|
  16.  
  17.  
  18. Now the translate Function simply converts the decimal value of
  19. the 4-bit nibble into an ascii hex value.  if you look at an
  20. ascii Chart you will see how this is done:
  21.  
  22. '0' = 48   '5' = 53    'A' = 65
  23. '1' = 49   '6' = 54    'B' = 66
  24. '2' = 50   '7' = 55    'C' = 67
  25. '3' = 51   '8' = 56    'D' = 68
  26. '4' = 52   '9' = 57    'E' = 69
  27.                        'F' = 70
  28.  
  29.  
  30. As you can see it easy For 0-9, you just add 48 to the value and
  31. it's converted, but when you go to convert 10 to A, you need to
  32. use a different offset, so For values above 9 you add 55.
  33.  
  34. Function Translate(B : Byte) : Char;
  35.   begin
  36.   if B < 10 then
  37.     Translate := Chr(B + 48)
  38.   else
  39.     Translate := Chr(B + 55);
  40.   end;
  41.